const request=require('../../../utils/request')
import {goods,goodsReputation,login_email,login_wx,goodsFavPut,goodsFavDelete,goodsFavCheck,goodsFavList,
  shoppingCart,    //加入购物车
  goodsDetail,     //数据详情
  qrcodeUnlimit
} from "../../../utils/apifm-wxapi"
var WxParse = require('../../wxParse/wxParse.js');
// const WXAPI = require('../../../utils/apifm-wxapi');
// WXAPI.init('hejiang');
var app=getApp()
Page({
  data: {    //页面的初始数据
    categoryId:'123181',
    index:[],
    iomge:[],
    iomme:[],
    arr:'24948',
    collect:-1,
    token:'',    
    id:"",     
    goodsId:'',
    options:{},
    length:"",                     //
    actionSheetHidden: true,
    num: 1,
    show: false,
    qrcodeUnlimit:''
  },
  onLoad: function (options) {    //生命周期函数--监听页面加载
    this.data.options=options
    this.data.goodsId=options.id
    
    this.request()                    //获取数据详情
    this.token()                      //获取token
    this.goodsFavCheck()              //检测是否已收藏
    this.goodsFavList()               //获取当前商品的收藏下标 id
  },
  onClickShow() {
    this.setData({ show: true });
    this.qrcodeUnlimit()
  },
  onClickHide() {
    this.setData({ show: false });
  },
  qrcodeUnlimit(){
    qrcodeUnlimit(this.data.goodsId+','+wx.getStorageSync('uid'),1).then((res)=>{
      console.log(res);
      console.log(this.data.iomme)
      if(res.code!=0){
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
        return
      }
      this.setData({
        show:true,
        qrcodeUnlimit:res.data
      })
//       console.log(this.data.qrcodeUnlimit);
    })
  },



  actionSheetTap() {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  actionSheetTa2() {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  noop() {},
  actionSheetChange: function(e) {
    var token=wx.getStorageSync('token')
    var arr=[this.data.iomme]
    // console.log(this.data.goodsId)
    // console.log(this.data.num)
    // console.log(arr)
    // console.log(token)
    shoppingCart(this.data.goodsId,this.data.num,arr,token).then(res=>{
      // console.log(res)
      this.setData({
        actionSheetHidden: !this.data.actionSheetHidden
      })
    })
  },
  bindMinus: function() {      //点击减号
		var num = this.data.num;
		if (num > 1) {// 如果大于1时，才可以减
			num --;
		}
		this.setData({// 将数值与状态写回
			num: num,
		});
	},
	bindPlus: function() {       //点击加号
		var num = this.data.num;
		num ++;// 不作过多考虑自增1
		this.setData({// 将数值与状态写回
			num: num,
		});
  },
  request(){
    request(`/hejiang/shop/goods/detail?id=${this.data.options.id}`).then(res=>{
      // console.log(res.data.data)
      WxParse.wxParse('article', 'html',res.data.data.content, this, 5);
      this.data.iomme=res.data.data.basicInfo;       
      this.setData({
        index:res.data.data.pics,
        iomge:res.data.data.content,
        iomme:res.data.data.basicInfo,           
      })        
      // console.log(this.data.index)  
      // console.log(this.data.iomge)  
      // console.log(this.data.iomme)  
    })
  },
  goodsFavList(){                        
    goodsFavList({token:this.data.token}).then(res=>{
      // console.log(res.data.length)
      this.data.length=res.data.length
      var ar={}
      res.data.forEach((item)=>{
        if (item.goodsId==this.data.goodsId) {
          ar=item
        }
      })
      this.data.id=ar.id
    })
  },
  goodsFavCheck(){                           //检测是否已收藏
    goodsFavCheck(this.data.token,this.data.goodsId).then(res=>{
      this.setData({
        collect:res.code
      })
    })
  },
  collect1(){                                 //添加收藏          
    goodsFavPut(this.data.token,this.data.goodsId).then(res=>{
      wx.showToast({
        title: '收藏成功',
        icon: 'success',
        duration: 2000
      })
      this.setData({
        collect:0
      })
    })
  },
  collect2(){                                 //删除收藏
    goodsFavDelete(this.data.token,this.data.id,this.data.goodsId).then(res=>{
      wx.showToast({
        title: '删除收藏',
        icon: 'success',
        duration: 2000
      })
      this.setData({
        collect:-1
      })
    })
  },
  token(){            //获取本地存储token
    this.data.token=wx.getStorageSync('token');
  },
  shopping(){
    wx.reLaunch({
      url: `/pages/shopping/shopping`,
    })
  },
  //分享
  share(e){
    console.log(e)
    // wx.showShareMenu({
    //   withShareTicket:true
    // })
    Page.onShareAppMessage()
    // console.log('12345')
  },
  
  //生成小程序
  createApplet(){
    // this.setData({
    //   show: true
    // })
    // this.show = true;
    console.log('12345')

  },
  onReady: function () {    //生命周期函数--监听页面初次渲染完成

  },
  onShow: function () {    //生命周期函数--监听页面显示

  },
  onHide: function () {    //生命周期函数--监听页面隐藏

  },
  onUnload: function () {   //生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {    //页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {  //页面上拉触底事件的处理函数

  },
  onShareAppMessage: function () {   //用户点击右上角分享

  }
})
